<?php

namespace app\common\model\xiluedu;

use app\admin\model\xiluedu\CouponCourse;
use app\common\library\Auth;
use app\common\model\User;
use think\Model;


class UserCoupon extends Model
{

    

    

    // 表名
    protected $name = 'xiluedu_user_coupon';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        //'start_time_text',
        //'end_time_text',
        'use_status_text'
    ];
    


    public function getUseStatusList()
    {
        return ['0' => __('未使用'), '1' => __('已使用')];
    }

    public function getStartTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['start_time']) ? $data['start_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i", $value) : $value;
    }

    public function getEndTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['end_time']) ? $data['end_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i", $value) : $value;
    }


    public function getUseStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['use_status']) ? $data['use_status'] : '');
        $list = $this->getUseStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function coupon(){
        return $this->belongsTo("Coupon",'coupon_id','id')->setEagerlyType(0);
    }

    /**
     * 根据条件获取已领取数量
     */
    public static function getCouponCount($where){
        return self::with(['coupon'])->where($where)->count();
    }
    /**
     * 获取用户优惠券
     */
    public static function get_user_coupon($params){
        $auth = Auth::instance();
        $user_id = $auth->id;
        //$tab = $params['tab'] ?? 'unuse';
        $pagesize = $params['pagesize'] ?? 10;
//        switch($tab){
//            case 'used':
//                #已使用
//                $where['use_status'] = '1';
//                $order = 'usetime desc';
//                break;
//            case "expire":
//                #过期
//                $where['use_status'] = '0';
//                $where['coupon.use_end_time'] = ['lt',time()];
//                $order = 'createtime desc';
//                break;
//            default:
//                $where['use_status'] = '0';
//                //$where['start_time'] = ['lt',time()];
//                $where['coupon.use_end_time'] = ['gt',time()];
//                $order = 'end_time asc';
//        }
        $where['user_id'] = $user_id;
        return static::with(['coupon'])
            //->where('user_id',$user_id)
            ->where($where)
            ->order('coupon.use_end_time','desc')
            ->paginate($pagesize);
    }


    /**
     * 获取可使用优惠券
     */
    public static function getCanCoupon($course_id){
        $user_id = (Auth::instance())->id;

        $sub = CouponCourse::where('course_id',$course_id)
            ->field('coupon_id')
            ->buildSql();
        $lists = static::alias("user_coupon")
            ->field("
            coupon.id,coupon.name,coupon.type,coupon.discount,coupon.at_least,coupon.money,coupon.range_status,
            user_coupon.id user_coupon_id,user_coupon.coupon_id,user_coupon.use_status")
            ->join('XilueduCoupon coupon','coupon.id=user_coupon.coupon_id')
            ->where('coupon.use_start_time','lt',time())
            ->where('coupon.use_end_time','gt',time())
            ->where(function ($query) use($course_id,$sub){
                $query->where('coupon.range_status','1')
                    ->whereOr("coupon.range_status = 2 AND coupon.id IN $sub");

            })
            ->where('user_coupon.user_id',$user_id)
            ->where('user_coupon.use_status','0')
            ->order('coupon.money','desc')
            ->select();
        return $lists;
    }

}
